package org.jsets.fastboot.dsc.controller;

import org.jsets.fastboot.database.DatabaseType;
import org.jsets.fastboot.dsc.model.dto.DataSourceConfigQuery;
import org.jsets.fastboot.dsc.model.entity.DataSourceConfig;
import org.jsets.fastboot.dsc.service.IDataSourceConfigService;
import org.jsets.fastboot.model.BaseResp;
import org.jsets.fastboot.model.DataResp;
import org.jsets.fastboot.model.ListResp;
import org.jsets.fastboot.security.annotation.HMACAuthenticate;
import org.jsets.fastboot.security.annotation.HasPermission;
import org.jsets.fastboot.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@RestController
@RequestMapping("/dsc/data_sources")
@Api(tags = "数据源")
public class DataSourceConfigController {

	@Autowired
	private IDataSourceConfigService service;
	
	@PostMapping(value= "/testing",  produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("测试数据源")
	public BaseResp testing(@RequestBody @Validated DataSourceConfig entity) {
		this.service.testing(entity);
		return BaseResp.ok();
	}
	
	@HasPermission("users:post")
	@HMACAuthenticate
	@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("增加数据源")
	public BaseResp insert(@RequestBody @Validated DataSourceConfig entity) {
		boolean present = this.service.findByName(entity.getName()).isPresent();
		Assert.isTrue(!present, String.format("存在名称为[%s]的数据源，请更换 ", entity.getName()));
		this.service.save(entity);
		return BaseResp.ok();
	}

	@HasPermission("users:delete")
	@DeleteMapping(value="/{id}" ,produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("删除数据源")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="主键", required=true, dataType = "Long", dataTypeClass = Long.class, paramType="path")
	})
	public BaseResp delete(@PathVariable("id") Long id) {
		Assert.notNull(id, "主键(id)不能为空");
		this.service.removeById(id);
		return BaseResp.ok();
	}

	@HasPermission("users:put")
	@PutMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("修改数据源")
	public BaseResp update(@RequestBody DataSourceConfig entity) {
		this.service.findByName(entity.getName()).ifPresent(t->{
			Assert.isTrue(StringUtils.equals(t.getId(), entity.getId()), String.format("存在名称为[%s]的数据源，请更换 ", entity.getName()));
		});
		this.service.updateById(entity);
		return BaseResp.ok();
	}
	
	@GetMapping(value="/{id}" ,produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("获取用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="主键", required=true, dataType = "String", dataTypeClass = String.class, paramType="path")
	})
	public DataResp<DataSourceConfig> get(@PathVariable("id") String id) {
		DataSourceConfig entity = this.service.getById(id);
		DataResp<DataSourceConfig> resp = DataResp.ok();
		resp.setData(entity);
		return resp;
	}

	@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("查询数据源")
	@ApiImplicitParams({
		 @ApiImplicitParam(name="page_no", value="当前页码", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="page_size", value="每页条数", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="driver_type", value="姓名", required=false, dataType = "String", dataTypeClass = String.class,  paramType="query")
		,@ApiImplicitParam(name="name", value="姓名", required=false, dataType = "String", dataTypeClass = String.class,  paramType="query")
		,@ApiImplicitParam(name="url", value="连接地址", required=false, dataType = "String", dataTypeClass = String.class,  paramType="query")
	})
	public ListResp<DataSourceConfig> list(
			@RequestParam(name = "page_no", required = false) Integer pageNo,
			@RequestParam(name = "page_size", required = false) Integer pageSize,
			@RequestParam(name = "account", required = false) String account,
			@RequestParam(name = "driver_type", required = false) String driverType,
			@RequestParam(name = "name", required = false) String name,
			@RequestParam(name = "url", required = false) String url) {

		DataSourceConfigQuery criteria = new DataSourceConfigQuery()
				.setCurrent(pageNo)
				.setSize(pageSize)
				.setName(name)
				.setDriverType(DatabaseType.of(driverType));
		ListResp<DataSourceConfig> respond = ListResp.ok();
		if(criteria.isPageNecessary()) {
			Page<DataSourceConfig> result = this.service.selectPage(criteria);
			respond.setData(result.getRecords());
			respond.setTotalCount(result.getTotal());
		}else {
			respond.setData(this.service.selectList(criteria));
		}
		return respond;
	}
}